package com.tpc.client.modules.core.document.datasource;

import com.tpc.dao.extend.Document;
import com.tpc.client.systeme.core.datasource.GwtRpcDataSource;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.client.modules.core.document.service.DocumentService;
import com.tpc.client.modules.core.document.service.DocumentServiceAsync;
import com.tpc.client.systeme.core.Systeme;

public class DocumentListDS extends GwtRpcDataSource {

    String strurl = null;
    Systeme systeme = null;
    String MODULE;

    public DocumentListDS(){
        
    }

    public DocumentListDS(String strID) {
            setID(strID);
            DataSourceField fldDocumentId = new DataSourceField("document_liste_id", FieldType.INTEGER);
            fldDocumentId.setPrimaryKey(Boolean.TRUE);
            fldDocumentId.setHidden(Boolean.TRUE);
            DataSourceTextField fldDocumentName = new DataSourceTextField("document_liste_name");
            DataSourceTextField fldDocumentDescription = new DataSourceTextField("document_liste_notes");
            fldDocumentDescription.setHidden(Boolean.TRUE);
            DataSourceTextField fldDocumentPath = new DataSourceTextField("document_liste_path");
            fldDocumentPath.setHidden(Boolean.TRUE);
            DataSourceTextField fldDocumentSize = new DataSourceTextField("document_liste_size");
            DataSourceTextField fldDocumentType = new DataSourceTextField("document_liste_type");
            DataSourceTextField fldDocumentUploaded = new DataSourceTextField("document_liste_uploaded");

            setFields(fldDocumentId,fldDocumentName,fldDocumentDescription,fldDocumentPath,fldDocumentSize,fldDocumentType,fldDocumentUploaded);

            strurl = Page.getAppDir();     
            strurl = strurl.substring(0,strurl.length()-1);
            int last_slash = strurl.lastIndexOf("/");
            if (last_slash > 8) {
                strurl = strurl.substring(strurl.lastIndexOf("/"));
            }

    }

    public void setObjects(Systeme cacheParam,String MODULEp) {
        systeme = cacheParam;
        MODULE = MODULEp;
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        if (DOM.getElementById("splashScreenText") != null) {
            DOM.getElementById("splashScreenText").setInnerHTML("contacts ...");
        }
        Criteria criteria = request.getCriteria();
        String strId = "-1";
        if (criteria != null) {
           strId = criteria.getAttribute("id_contact");
        }
        DocumentServiceAsync service = GWT.create(DocumentService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(strurl+"/com.tpc.document/docloader");
        service.loadList(strId,new AsyncCallback<Document[]> () {
            public void onFailure (Throwable caught) {
                systeme.handleServerException(caught);
                systeme.hideSplashScreen();
                systeme.stopLoading();
            }
            public void onSuccess (Document[] result) {
                int NbrResultat = 0;
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[result.length];
                    NbrResultat = result.length;
                    for (int i = 0; i < list.length; i++) {
                        list[i] = new ListGridRecord();
                        list[i].setAttribute("document_liste_id",result[i].getId());
                        list[i].setAttribute("document_liste_name",result[i].getFileName());
                        list[i].setAttribute("document_liste_notes",result[i].getDescription());
                        list[i].setAttribute("document_liste_path",result[i].getPath());
                        list[i].setAttribute("document_liste_size",result[i].getSize());
                        list[i].setAttribute("document_liste_type",result[i].getType());
                        list[i].setAttribute("document_liste_uploaded",result[i].getDateUpload());
                        String typeIcon = "emptydoc";
                        if (result[i].getType() != null) {
                            if (result[i].getType().indexOf("pdf") > -1) {
                                typeIcon = "pdfIcon"; 
                            }
                        }
                        list[i].setAttribute("document_liste_type_icon",typeIcon);
                        list[i].setAttribute("document_liste_download_icon","duplicate");
                        
                    }
                }
                //systeme.getAppBar().getTab(MODULE).setTitle(systeme.getDictionary().MODULE_DOCUMENT()+" ("+NbrResultat+")");
                response.setData(list);
                processResponse(requestId, response);
                systeme.hideSplashScreen();
                systeme.stopLoading();
            }
        });
    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //@TODO : fdsa
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

}